/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.input;

import com.android1.amarena2d.engine.EngineObject;


public abstract class DoubleTabDetector extends EngineObject implements TouchListener {

    public static final float DEFAULT_THRESHOLD = 0.4F ;

    private float threshold = DEFAULT_THRESHOLD;
    private boolean exclusive;
    private float lastTab;

    public DoubleTabDetector() {
    }

    public DoubleTabDetector(float threshold) {
        this.threshold = threshold;
    }

    public boolean isExclusive() {
        return exclusive;
    }

    /**
     * If true, will consume the event and no other touchlistener will be notified.
     *
     * @param exclusive
     * @return
     */
    public DoubleTabDetector setExclusive(final boolean exclusive) {
        this.exclusive = exclusive;
        return this;
    }

    public abstract void onDoubleTab(float x, float y, int pointer);

    @Override
    public boolean onTouchDown(float x, float y, int pointer, boolean isHit) {
        if (isHit) {
            if (lastTab != 0 && lastTab + threshold > engine.time) {
                onDoubleTab(x, y, pointer);
            }
            lastTab = engine.time;
        }
        return exclusive;
    }

    @Override
    public boolean onTouchUp(float x, float y, int pointer, boolean isHit) {
        return false;
    }

    @Override
    public boolean onTouchDragged(float x, float y, int pointer, boolean isHit) {
        return false;
    }

}